package chapter1.date.task;
/*
8：输入某人的生日（月-日格式），计算具体下个生日还有多少天（2-29可以单独处理）
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Task8 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Calendar calendar1 = Calendar.getInstance();    //现在的时间
        Calendar calendar2 = Calendar.getInstance();    //生日的时间
        SimpleDateFormat a = new SimpleDateFormat("MM-dd");
        System.out.print("请输入生日（月-日格式）：");
        String birthday = input.next();
        try {
            Date date = a.parse(birthday);     //String=>Date
            calendar2.setTime(date);    //Date ->Calendar
            calendar2.set(Calendar.YEAR, calendar1.get(Calendar.YEAR));
            long millisecond1 = calendar1.getTimeInMillis();    
            long millisecond2 = calendar2.getTimeInMillis();    
            long day1 = 0;    //相差毫秒的天数
            if (millisecond1 >= millisecond2) { //今年生日已过
                day1 = (millisecond1 - millisecond2) / (24 * 60 * 60 * 1000);
                long dayOfYear = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
                long day = dayOfYear - day1;
                System.out.println("距离下个生日还有" + day + "天");
            } else {   //今年生日还没到
                day1 = (millisecond2 - millisecond1) / (24 * 60 * 60 * 1000);   
                System.out.println("距离下个生日还有" + day1 + "天");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
